import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		userInfo: {}, // 用户信息
		__key_member: 'usemall_userInfo', // 用户信息缓存keyName
		token: '',  // token 令牌
		__key_token: 'uni_id_token' // token缓存keyName
	},
	mutations: {
		// 保存用户数据
		saveUserInfo(state, data) {
			state.userInfo = Object.assign(state.userInfo, data);
			console.log('newUserInfo', state.userInfo)
			uni.setStorage({
				key: state.__key_member,
				data: state.userInfo
			})
		},
		// 登出
		logout(state, data) {
			state.hasLogin = false;
			state.userInfo = {};
			state.token = '';
			uni.removeStorageSync(state.__key_member)
			uni.removeStorageSync(state.__key_token)
		},
		// 缓存token数据
		sessionToken(state, token) {
			state.token = token;
			uni.setStorage({
				key: state.__key_token,
				data: state.token
			})
			state.islogin = true;
		},
		// 从缓存中加载数据保存至store
		loadSession(state) {
			if (uni.getStorageSync(state.__key_member)) state.userInfo = uni.getStorageSync(state.__key_member);
			if (uni.getStorageSync(state.__key_token)) state.token = uni.getStorageSync(state.__key_token);
			if (state.token) state.islogin = true;
		}
		
	}
})
export default store
